package com.zhengyisky.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhengyisky.entity.RequestEnity;
import com.zhengyisky.service.UserService;
import com.zhengyisky.utils.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class BankController {

    @Value("${aibank.platformNo}")
    private String platformNo;

    @Value("${key.publicKey}")
    private String publicKey;

    @Value("${key.privateKey}")
    private String privateKey;

    @Autowired
    private UserService userService;


    @PostMapping("/initPersonRegisterParam/{mobile}")
    @ResponseBody
    public RequestEnity initPersonRegisterParam(@PathVariable String mobile) throws Exception {
        RequestEnity requestEnity = new RequestEnity();
        requestEnity.setServiceName("PERSONAL_REGISTER_EXPAND");
        requestEnity.setPlatformNo(platformNo);
        requestEnity.setUserDevice("MOBILE");
        requestEnity.setKeySerial(1);
        Map map = new HashMap();
        map.put("platformUserNo",mobile);
        map.put("requestNo", UUID.randomUUID().toString());
        map.put("userRole","BORROWERS");
        map.put("checkType","LIMIT");
        map.put("redirectUrl","http://localhost/resultPersonRegister");
        String reqData = JSONObject.toJSONString(map);
        requestEnity.setReqData(reqData);
        requestEnity.setSign(RSAUtils.encrypt(reqData.substring(0,100),publicKey));
        return requestEnity;
    }

    @GetMapping("/notifyResult")
    @ResponseBody
    public String notifyResult(HttpServletRequest request) throws Exception {
        System.out.println("接收到异步回调");

        String serviceName = request.getParameter("serviceName");
        //验签
        String sign = request.getParameter("sign");
        sign = sign.replaceAll(" ","+");
        String respData = request.getParameter("respData");
        String subData = respData.length()>100?respData.substring(0,100):respData;
        System.out.println(sign);
        if(!subData.equals(RSAUtils.decrypt(sign,privateKey))){
            return null;
        }
        //解析respData
        if(serviceName.equals("PERSONAL_REGISTER_EXPAND")){
            //个人开户接口的异步回调
            //判断是否成功，如果成功修改用户状态为1---》2
            Map map = JSONObject.parseObject(respData, Map.class);
            Integer code = (Integer) map.get("code");
            if(code==0){
                //开户成功
                String platformUserNo = (String) map.get("platformUserNo");
                userService.updateUserStatus(platformUserNo,2);
            }
        }


        return "SUCCESS";
    }

    @GetMapping("/resultPersonRegister")
    public String resultPersonRegister(HttpServletRequest request){
        String sign = request.getParameter("sign");
        System.out.println(sign);
        String respData = request.getParameter("respData");
        System.out.println(respData);
        //1、根据私钥+签名---》解密
        //2、判断解密的结果和respData是否一致
        //3、从respData中取出code
        //4、判断code是不是0

        // 修改用户状态   已开户


        return "redirect:/accountSuccess.html";
    }

}
